﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsTemplateFileBackUp
    {
        private readonly KeRui.DAL.CmsTemplateFileBackUp dal = new KeRui.DAL.CmsTemplateFileBackUp();

        public int Add(KeRui.Model.CmsTemplateFileBackUp model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsTemplateFileBackUp> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsTemplateFileBackUp> list = new List<KeRui.Model.CmsTemplateFileBackUp>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsTemplateFileBackUp item = new KeRui.Model.CmsTemplateFileBackUp();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["FileID"].ToString() != "")
                    {
                        item.FileID = int.Parse(dt.Rows[i]["FileID"].ToString());
                    }
                    if (dt.Rows[i]["TemplateID"].ToString() != "")
                    {
                        item.TemplateID = int.Parse(dt.Rows[i]["TemplateID"].ToString());
                    }
                    item.FileName = dt.Rows[i]["FileName"].ToString();
                    item.FilePath = dt.Rows[i]["FilePath"].ToString();
                    item.FileContent = dt.Rows[i]["FileContent"].ToString();
                    if (dt.Rows[i]["FileType"].ToString() != "")
                    {
                        item.FileType = int.Parse(dt.Rows[i]["FileType"].ToString());
                    }
                    if (dt.Rows[i]["FileUpdateInterval"].ToString() != "")
                    {
                        item.FileUpdateInterval = int.Parse(dt.Rows[i]["FileUpdateInterval"].ToString());
                    }
                    if (dt.Rows[i]["UsingNum"].ToString() != "")
                    {
                        item.UsingNum = int.Parse(dt.Rows[i]["UsingNum"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["IsTop"].ToString() != "")
                    {
                        item.IsTop = int.Parse(dt.Rows[i]["IsTop"].ToString());
                    }
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    item.UserName = dt.Rows[i]["UserName"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ID)
        {
            this.dal.Delete(ID);
        }

        public int GetCount(string strWhere)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsTemplateFileBackUp", strWhere);
        }

        public List<KeRui.Model.CmsTemplateFileBackUp> GetList(string strWhere, int PageSize, int PageIndex)
        {
            string strOrder = " UpdateTime DESC";
            DataSet set = new KeRui.DAL.Common().GetList("CmsTemplateFileBackUp", PageSize, PageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsTemplateFileBackUp> GetListByCache(string strWhere, int PageSize, int PageIndex)
        {
            string cacheKey = string.Concat(new object[] { "CmsTemplateFileBackUpList-", strWhere, PageSize, PageIndex });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strWhere, PageSize, PageIndex);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsTemplateFileBackUp>) cache;
        }

        public KeRui.Model.CmsTemplateFileBackUp GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.CmsTemplateFileBackUp GetModelByCache(int ID)
        {
            string cacheKey = "CmsTemplateFileBackUpModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsTemplateFileBackUp) cache;
        }

        public void Update(KeRui.Model.CmsTemplateFileBackUp model)
        {
            this.dal.Update(model);
        }
    }
}

